﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DistributeTransaction.DAL;
using DistributeTransaction.Models;
using DistributeTransaction.Utils;
using log4net;

namespace DistributeTransaction.Controllers
{ 
    public class WatchListController : BaseController
    {
        private ILog log = LogManager.GetLogger(typeof(WatchListController));
        private WatchListRepository watchListRepository;

        public WatchListController()
        {
            watchListRepository = new WatchListRepository(DBContext);
        }
        //
        // GET: /WatchLists/

        public ViewResult Index()
        {
            string ids = TempData["data"] as string;
            if (!string.IsNullOrEmpty(ids))
            {
                watchListRepository.UpdateWatchList(ParseProductIds(ids), CustomerId);
            }

            return View(watchListRepository.GetWatchList(CustomerId));
        }

       
        [HttpPost]
        [ActionName("Index")]
        [AcceptSubmitType(Name = "Command", Type = "Delete")]
        public ActionResult Delete(FormCollection formCollection)
        {
            IEnumerable<int> list = ParseProductIds(formCollection);
            watchListRepository.Remove(list, CustomerId);
            return RedirectToAction("index", "WatchList");
        }

        [HttpPost]
        [ActionName("Index")]
        [AcceptSubmitType(Name = "Command", Type = "Add To ShoppingCart")]
        public ActionResult AddToShoppingCar(FormCollection formCollection)
        {
            IEnumerable<int> productIds = ParseProductIds(formCollection);

            ShoppingCartRepository shoppingCartRepository = new ShoppingCartRepository(DBContext);
            shoppingCartRepository.UpdateShoppingCart(productIds, CustomerId);

            return RedirectToAction("Index", "ShoppingCart");
        }
    }
}